package com.android.packageinstaller;

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.net.Uri;
import android.os.IBinder;
import android.annotation.Nullable;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MyIntentService extends IntentService {

    private final String TAG = "zzzz";
    private @Nullable File mStagedFile;
    private int Startflags = 0;
    private int Fileflags = 0;
    private String[] uriString;
    private String[] uriStringFile = new String[9];
    private String[] uriStringCopy = new String[9];

    public MyIntentService() {
        super("MyIntentService");
    }


    @Override
    protected void onHandleIntent(Intent intent) {
        String action = intent.getExtras().getString("param");
        //Log.i(TAG,"onHandleIntent Startflags = "+Startflags);
        uriStringCopy[Startflags] = action;
        Startflags++;
        

    }

    private @Nullable boolean doinStream(Uri packageUri){
        try (InputStream in = getContentResolver().openInputStream(packageUri)) {
            // Despite the comments in ContentResolver#openInputStream the returned stream can
            // be null.
            if (in == null) {
                return false;
            }
            mStagedFile = TemporaryFileManager.getStagedFile(this);

            try (OutputStream out = new FileOutputStream(mStagedFile)) {
                byte[] buffer = new byte[1024 * 1024];
                int bytesRead;
                while ((bytesRead = in.read(buffer)) >= 0) {
                    out.write(buffer, 0, bytesRead);
                }
            }
        } catch (IOException | SecurityException | IllegalStateException e) {
            //Log.w(TAG, "Error staging apk from content URI", e);
            return false;
        }
        //Log.d(TAG, "mStagedFile.getAbsolutePath() = "+mStagedFile.getAbsolutePath());
        uriStringFile[Fileflags] = mStagedFile.getAbsolutePath();
        Fileflags++;
        return true;
    }

    @Override
    public IBinder onBind(Intent intent) {
        //Log.i(TAG,"onBind");
        return super.onBind(intent);
    }

    @Override
    public void onCreate() {
        //Log.i(TAG,"onCreate");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //Log.i(TAG,"onStartCommand flags = "+flags+" startId = "+startId);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void setIntentRedelivery(boolean enabled) {
        super.setIntentRedelivery(enabled);
        //Log.i(TAG,"setIntentRedelivery");
    }

    @Override
    public void onDestroy() {
        //Log.i(TAG,"onDestroy");
        doForString();
        doForFileString();
        //Log.i(TAG,"onDestroy end");
        super.onDestroy();
    }
    
    private void doForString(){
        //Log.i(TAG,"doForString >> Startflags = "+Startflags);
        for(int i = 0;i < Startflags;i++){
            Uri uri = Uri.parse(uriStringCopy[i]);
            boolean b = doinStream(uri);
            //Log.i(TAG,"b = "+b);
        }
    }
    
    private void doForFileString(){
        //Log.i(TAG,"doForFileString >> Fileflags = "+Fileflags);
        for(int i = 0;i < Fileflags;i++){
            //Log.i(TAG,"uriStringFile = "+uriStringFile[i]);
            SilentInstaller.install(this, uriStringFile[i]);
        }
    }

}
